/*
 * StyleOps.java
 * 
 * Created by demory on Feb 14, 2010, 1:08:04 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.modules;

import org.fpdev.apps.cart.CartEvent;
import org.fpdev.apps.cart.CartEventResult;
import org.fpdev.apps.cart.CartModule;
import org.fpdev.apps.cart.Cartographer;
import org.fpdev.apps.cart.Editor;
import org.fpdev.apps.cart.actions.CopyLineStyleAction;
import org.fpdev.apps.cart.actions.CreateLineStyleAction;
import org.fpdev.apps.cart.actions.DeleteLineStyleAction;
import org.fpdev.apps.cart.actions.EditStyleAction;
import org.fpdev.apps.cart.gui.EditStyleDialog;
import org.fpdev.apps.cart.network.LineStyle;

/**
 *
 * @author demory
 */
public class StyleOps extends CartModule {

  public StyleOps(Cartographer cart) {
    super(cart);
  }

  @Override
  public ID getID() {
    return ID.STYLE;
  }

  public CartEventResult handleEvent(CartEvent event) {

    Editor ed = event.getEditor();
    LineStyle style = (LineStyle) event.getProperty("style");

    switch(event.getType()) {
      case CREATE_STYLE: return createStyle(ed);
      case DELETE_STYLE: return deleteStyle(style, ed);
      case EDIT_STYLE: return editStyle(style, ed);
      case COPY_STYLE: return copyStyle(style, ed);
    }

    return null;
  }

  private CartEventResult createStyle(Editor ed) {
    CartEventResult result = new CartEventResult();
    result.setAction(new CreateLineStyleAction(ed));
    return result;
  }

  private CartEventResult deleteStyle(LineStyle style, Editor ed) {
    CartEventResult result = new CartEventResult();
    result.setAction(new DeleteLineStyleAction(style, ed));
    return result;
  }

  private CartEventResult editStyle(LineStyle style, Editor ed) {
    CartEventResult result = new CartEventResult();
    cart_.getGUI().getControlFrameManager().getStylesFrame().refreshList();
    EditStyleDialog dialog = new EditStyleDialog(cart_.getGUI(), style);
    if(dialog.okPressed()) {
      // create action
      EditStyleAction action = new EditStyleAction(ed, style, dialog.getStyleName(), dialog.getSubStyles());
      result.setAction(action);
    }
    return result;
  }

  private CartEventResult copyStyle(LineStyle style, Editor ed) {
    CartEventResult result = new CartEventResult();
    result.setAction(new CopyLineStyleAction(ed, style.getCopy()));
    return result;
  }

}
